﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace CustomHttpHandler
{
    public class Handler : IHttpHandler
    {
        public Handler()
        {

        }

        #region Implementation of IHttpHandler
        public void ProcessRequest(System.Web.HttpContext context)
        {
            HttpResponse objResponse = context.Response;
            HttpSessionState objSession = context.Session;
            int loop1;

            // Load NameValueCollection object.
            System.Collections.Specialized.NameValueCollection coll = context.Request.QueryString;
            // Get names of all keys into a string array.
            String[] arr1 = coll.AllKeys;
            StringBuilder sb = new StringBuilder();

            //kolla vilken url
            if (context.Request.RawUrl.ToUpper().Contains("COSTANSWER"))
            {
                sb.Append("costAnswer.aspx");
                for (loop1 = 0; loop1 < arr1.Length; loop1++)
                {
                    if (arr1[loop1] == "claimID")
                    {
                        String[] arr2 = coll.GetValues(arr1[loop1]);
                        int claimID = Convert.ToInt32(arr2[0]);
                        sb.Append(string.Format("?claimID={0}&", claimID));
                    }
                    else if (arr1[loop1] == "vehicleID")
                    {
                        String[] arr2 = coll.GetValues(arr1[loop1]);
                        int vehicleID = Convert.ToInt32(arr2[0]);
                        sb.Append(string.Format("vehicleID={0}", vehicleID));
                    }
                }

                context.Response.Write(sb.ToString());
                context.Response.Redirect(sb.ToString());
            }
            if (context.Request.RawUrl.ToUpper().Contains("COSTREPORT"))
            {
                sb.Append("costReport.aspx");
                context.Response.Redirect(sb.ToString());
            }
        }


        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
        #endregion
    }

}
